<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>结算页</title>

	<link rel="stylesheet" href="../js/bootstrap/css/bootstrap.min.css" rel="stylesheet">

	<link rel="stylesheet" type="text/css" href="../js/shop/css/webbase.css" />
	<link rel="stylesheet" type="text/css" href="../js/shop/css/pages-getOrderInfo.css" />



</head>

<body>
<!--head-->
<div class="top">
	<div class="py-container">
		<div class="shortcut">
			<ul class="fl">
				<li class="f-item">品优购欢迎您！</li>
				<li class="f-item"><a href="login.html">请登录</a>　<span><a href="reg.html">免费注册</a></span></li>
			</ul>
			<ul class="fr">
				<li class="f-item">我的订单</li>
				<li class="f-item space"></li>
				<li class="f-item">我的品优购</li>
				<li class="f-item space"></li>
				<li class="f-item">品优购会员</li>
				<li class="f-item space"></li>
				<li class="f-item">企业采购</li>
				<li class="f-item space"></li>
				<li class="f-item">关注品优购</li>
				<li class="f-item space"></li>
				<li class="f-item">客户服务</li>
				<li class="f-item space"></li>
				<li class="f-item">网站导航</li>
			</ul>
		</div>
	</div>
</div>
<div class="cart py-container">
	<!--logoArea-->
	<div class="logoArea">
		<div class="fl logo"><span class="title">结算页</span></div>
		<div class="fr search">
			<form class="sui-form form-inline">
				<div class="input-append">
					<input type="text" type="text"   class="input-error input-xxlarge" placeholder="品优购自营" />
					<button class="sui-btn btn-xlarge btn-danger" type="button">搜索</button>
				</div>
			</form>
		</div>
	</div>
	<!--主内容-->
	<div class="checkout py-container">
		<div class="checkout-tit">
			<h4 class="tit-txt">填写并核对订单信息</h4>
		</div>
		<div class="checkout-steps">
			<!--收件人信息-->
			<div class="step-tit">
				<h5>收件人信息<span onclick="addAlert();" ><a >新增收货地址</a></span></h5>

			</div>
			<div class="step-cont">
				<div class="recipientInfo">
					<ul class="addr-detail">
						<li class="addr-item">

							<div id="recipientList">

							</div>


						</li>


					</ul>
					<!--添加地址-->
					<div  tabindex="-1" role="dialog" data-hasfoot="false" class="sui-modal hide fade edit">
						<div class="modal-dialog">
							<div class="modal-content">
								<div class="modal-header">
									<button type="button" data-dismiss="modal" aria-hidden="true" class="sui-close">×</button>
									<h4 id="myModalLabel" class="modal-title">添加收货地址</h4>
								</div>
								<div class="modal-body">
									<form action="" class="sui-form form-horizontal">
										<div class="control-group">
											<label class="control-label">收货人：</label>
											<div class="controls">
												<input type="text" class="input-medium">
											</div>
										</div>

										<div class="control-group">
											<label class="control-label">详细地址：</label>
											<div class="controls">
												<input type="text" class="input-large">
											</div>
										</div>
										<div class="control-group">
											<label class="control-label">联系电话：</label>
											<div class="controls">
												<input type="text" class="input-medium">
											</div>
										</div>

									</form>


								</div>
								<div class="modal-footer">
									<button type="button" data-ok="modal" class="sui-btn btn-primary btn-large">确定</button>
									<button type="button" data-dismiss="modal" class="sui-btn btn-default btn-large">取消</button>
								</div>
							</div>
						</div>
					</div>
					<!--确认地址-->
				</div>
				<div class="hr"></div>

			</div>
			<div class="hr"></div>
			<!--支付和送货-->
			<div class="payshipInfo">
				<div class="step-tit">
					<h5>支付方式</h5>
				</div>
				<div id="payDivList">
					<!--<div class="step-cont">
						<ul class="payType">
							<li class="selected">微信付款<span title="点击取消选择"></span></li>
							<li>货到付款<span title="点击取消选择"></span></li>
						</ul>
					</div>-->
				</div>

				<div class="hr"></div>
				<div class="step-tit">
					<h5>送货清单</h5>
				</div>
				<div class="step-cont" id="carList">
					<ul class="send-detail">
						<li>

						<!--	<div class="sendGoods">

								<ul class="yui3-g">
									<li class="yui3-u-1-6">
										<span><img src="img/goods.png"/></span>
									</li>
									<li class="yui3-u-7-12">
										<div class="desc">Apple iPhone 6s (A1700) 64G 玫瑰金色 移动联通电信4G手机硅胶透明防摔软壳 本色系列</div>
										<div class="seven">7天无理由退货</div>
									</li>
									<li class="yui3-u-1-12">
										<div class="price">￥5399.00</div>
									</li>
									<li class="yui3-u-1-12">
										<div class="num">X1</div>
									</li>
									<li class="yui3-u-1-12">
										<div class="exit">有货</div>
									</li>
								</ul>
							</div>-->
						</li>
						<li></li>
						<li></li>
					</ul>
				</div>
				<div class="hr"></div>
			</div>
			<div class="linkInfo">
				<div class="step-tit">
					<h5>发票信息</h5>
				</div>
				<div class="step-cont">
					<span>普通发票（电子）</span>
					<span>个人</span>
					<span>明细</span>
				</div>
			</div>
			<div class="cardInfo">
				<div class="step-tit">
					<h5>使用优惠/抵用</h5>
				</div>
			</div>
		</div>
	</div>
	<div class="order-summary" id="goodsInfoDiv">

	</div>
	<div class="clearfix trade">
		<div id="totalCountHtml">

		</div>
		<div id="defaultRecipientHtml">

		</div>
	</div>
	<div class="submit">
		<a class="sui-btn btn-danger btn-xlarge"  onclick="addOrder()">提交订单</a><!--href="pay.html"-->
	</div>
</div>
<!-- 底部栏位 -->
<!--页面底部-->
<div class="clearfix footer">
	<div class="py-container">
		<div class="footlink">
			<div class="Mod-service">
				<ul class="Mod-Service-list">
					<li class="grid-service-item intro  intro1">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item  intro intro2">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item intro  intro3">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item  intro intro4">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item intro intro5">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
				</ul>
			</div>
			<div class="clearfix Mod-list">
				<div class="yui3-g">
					<div class="yui3-u-1-6">
						<h4>购物指南</h4>
						<ul class="unstyled">
							<li>购物流程</li>
							<li>会员介绍</li>
							<li>生活旅行/团购</li>
							<li>常见问题</li>
							<li>购物指南</li>
						</ul>

					</div>
					<div class="yui3-u-1-6">
						<h4>配送方式</h4>
						<ul class="unstyled">
							<li>上门自提</li>
							<li>211限时达</li>
							<li>配送服务查询</li>
							<li>配送费收取标准</li>
							<li>海外配送</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
						<h4>支付方式</h4>
						<ul class="unstyled">
							<li>货到付款</li>
							<li>在线支付</li>
							<li>分期付款</li>
							<li>邮局汇款</li>
							<li>公司转账</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
						<h4>售后服务</h4>
						<ul class="unstyled">
							<li>售后政策</li>
							<li>价格保护</li>
							<li>退款说明</li>
							<li>返修/退换货</li>
							<li>取消订单</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
						<h4>特色服务</h4>
						<ul class="unstyled">
							<li>夺宝岛</li>
							<li>DIY装机</li>
							<li>延保服务</li>
							<li>品优购E卡</li>
							<li>品优购通信</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
					</div>
				</div>
			</div>
			<div class="Mod-copyright">
				<ul class="helpLink">
					<li>关于我们<span class="space"></span></li>
					<li>联系我们<span class="space"></span></li>
					<li>关于我们<span class="space"></span></li>
					<li>商家入驻<span class="space"></span></li>
					<li>营销中心<span class="space"></span></li>
					<li>友情链接<span class="space"></span></li>
					<li>关于我们<span class="space"></span></li>
					<li>营销中心<span class="space"></span></li>
					<li>友情链接<span class="space"></span></li>
					<li>关于我们</li>
				</ul>
			</div>
		</div>
	</div>
</div>
<!--页面底部END-->

<!--弹框式增加-->
<div id="addAlert" style="display: none">
	<form class="form-horizontal"><!--method="post"  action="/brand/addBrand.jhtml"-->
		<div class="form-group">
			<label class="col-sm-2 control-label">收货人:</label>
			<div class="col-sm-4">
				<input type="text" name="name" class="form-control" id="add_name" placeholder="请输入收货人">
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label">联系电话 :</label>
			<div class="col-sm-4">
				<input type="text" name="phone" class="form-control" id="add_phone" placeholder="请输入联系电话">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label">地址:</label>
			<div class="col-sm-4">
				<input type="text" name="recipientAddress" class="form-control" id="add_recipientAddress" placeholder="请输入地址">
			</div>

		</div>


	</form>
</div>

<!--收件人信息模板-->
<div id="recipientDiv" style="display: none">
	<div class="con name ##selected##" onclick="updateStatus('##id##')"><a href="javascript:;" >##name##<span title="点击取消选择">&nbsp;</a></div>
	<div class="con recipient" onclick="updateRecipient(this,'##name##','##recipientAddress##','##phone##')">##name## ##recipientAddress## <span>##phone##</span>
		##span_addr##
		<span style="display: none"><a  onclick="modifyRecipient('##id##')">编辑</a>&nbsp;&nbsp;<a href="javascript:;"  onclick="delerecipient('##id##')">删除</a></span>
	</div>
	<div class="clearfix"></div>
</div>
<!--送货清单模板-->
<div id="goodsDiv" style="display: none">
	<div class="sendGoods" >

		<ul class="yui3-g">
			<li class="yui3-u-1-6">
				<span><img src="##image##" ></span>
			</li>
			<li class="yui3-u-7-12">
				<div class="desc">##skuName##</div>
				<div class="seven">7天无理由退货</div>
			</li>
			<li class="yui3-u-1-12">
				<div class="price">##price##</div>
			</li>

			<li class="yui3-u-1-12">
				<div class="num">##count##</div>
			</li>
			<li class="yui3-u-1-12">
				<div class="exit">##status##</div>
			</li>
		</ul>
	</div>
</div>


<!--商品最后信息模板-->
<div style="display: none" id="cartDiv">
	<div class="static fr">
		<div class="list">
			<span><i class="number">##totalCount##</i>件商品，总商品金额</span>
			<em class="allprice">##totalPrice##</em>
		</div>
		<div class="list">
			<span>返现：</span>
			<em class="money">0.00</em>
		</div>
		<div class="list">
			<span>运费：</span>
			<em class="transport">0.00</em>
		</div>
	</div>
</div>

<!--弹框式修改-->
<div id="updateAddresDiv" style="display: none">
	<form class="form-horizontal"><!--method="post"  action="/brand/addBrand.jhtml"-->
		<div class="form-group">
			<label class="col-sm-2 control-label">收货人:</label>
			<div class="col-sm-4">
				<input type="hidden" name="id" id="update_id">
				<input type="hidden" name="memberId" id="update_memberId">
				<input type="text" name="name" class="form-control" id="update_name" placeholder="请输入收货人">
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label">联系电话 :</label>
			<div class="col-sm-4">
				<input type="text" name="phone" class="form-control" id="update_phone" placeholder="请输入联系电话">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label">地址:</label>
			<div class="col-sm-4">
				<input type="text" name="recipientAddress" class="form-control" id="update_recipientAddress" placeholder="请输入地址">
			</div>

		</div>


	</form>
</div>

<!--支付方式-->
<div id="payDiv" style="display: none">
	<div class="step-cont">
		<ul class="payType">
			<li class="##paySelected_1##" onclick="updatePay()">微信付款<span title="点击取消选择"></span></li>
			<li class="##paySelected_2##" onclick="updatePay()">货到付款<span title="点击取消选择"></span></li>
		</ul>
	</div>
</div>
<!--
class="con name ##selected##" onclick="updateStatus('##id##')"
-->



<script type="text/javascript" src="../js/shop/js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript" src="../js/shop/js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="../js/shop/js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="../js/shop/components/ui-modules/nav/nav-portal-top.js"></script>
<script type="text/javascript" src="../js/shop/js/pages/getOrderInfo.js"></script>



</body>

<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="../js/bootstrap/js/bootstrap.min.js"></script>

<script src="../js/bootbox/bootbox.locales.min.js"></script>
<script src="../js/bootbox/bootbox.min.js"></script>

<script src="../js/common.js"></script>
<script src="../js/jquery.cookie.min.js"></script>


<script>
	$(function (){
		findRecipientList();
		initCart();
		initPayDiv();
		findIdempotenceToken();
	})

	var v_IdempotenceToken;
	function findIdempotenceToken(){
		$.ajax({
			type:"post",
			url:server_url+"/tokens/createToken",
			beforeSend:function (xhr){
				let v_token=$.cookie("token");
				xhr.setRequestHeader("x-auth",v_token);
			},
			success:function (result){
				if(result.code==200){
					v_IdempotenceToken=result.data;
					console.log(result.data);
				}
			}
		})

	}

	//收件人id
	var m_recipientId;

	//支付方式默认值  1:微信	2:货到付款
	var m_payType=1;
	//点击触犯修改支付方式
	function updatePay(){
		if(m_payType==1){
			m_payType=2;
		}else if(m_payType==2){
			m_payType=1;
		}
		initPayDiv();
	}
	//初始化支付方式Div
	function initPayDiv(){
		var v_res="";
		var v_html = $("#payDiv").html();

		if(m_payType==1){
			v_res = v_html.replace(/##paySelected_1##/g,"selected");
		}else if(m_payType==2){
			v_res = v_html.replace(/##paySelected_2##/g,"selected");
		}

		$("#payDivList").html(v_res);
	}



	//提交订单(新增)
	function addOrder(){
		if(m_recipientId==undefined || undefined==0){
			alert("请选择收件人信息")
			return;
		}

		var param = {}
		param.recipientId=m_recipientId;
		param.payType=m_payType;
		console.log(param);
		$.ajax({
			type:"post",
			url:server_url+"/order/addOrder",
			beforeSend:function (xhr){
				let v_token=$.cookie("token");
				xhr.setRequestHeader("x-auth",v_token);
				xhr.setRequestHeader("x-IdempotenceToken",v_IdempotenceToken);
			},
			data:param ,
			success:function (result){
				if(result.code==200){
					alert(result.code)
					location.href="OrderList.html";
				}
			}
		})
	}




	function updateRecipient(obj,name,recipientAddress,phone){
		$("#defaultRecipientHtml").html('<div class="fc-receiverInfo">寄送至:'+name+' 收货人：'+recipientAddress+' '+phone+'</div>')
		$(".con.recipient").css("font-weight","");
		$(obj).css("font-weight","bold");
	}

	function updateStatus(recipientId){
		$.ajax({
			type:"post",
			url:server_url+"/recipient/updateStatus",
			beforeSend:function (xhr){
				let v_token=$.cookie("token");
				xhr.setRequestHeader("x-auth",v_token);
			},
			data: {"id":recipientId},
			success:function (result){
				if(result.code==200){
					// alert("更新ok")
					findRecipientList();
				}
			}
		})
	}

	function initEvent(){
		$(".con.recipient").hover(function (){
			$(this).children().last().show();
		},function (){
			$(this).children().last().hide();
		})
	}


	function initCart(){
		$.ajax({
			type:"get",
			url:server_url+"/cart/findCart",
			beforeSend:function (xhr){
				let v_token=$.cookie("token");
				xhr.setRequestHeader("x-auth",v_token);
			},
			success:function (result){
				if(result.code==200){
					let  v_data=result.data;
					let  v_cartList=v_data.cartItemVoList;
					console.log(v_cartList);
					var v_res="";
					for (let v_cart of v_cartList){
						var v_html = $("#goodsDiv").html();

						var v_status="有货";
						if(v_cart.count<=0){
							v_status="缺货"
						}

						v_res += v_html.replace(/##image##/g,v_cart.image)
								.replace(/##price##/g,"￥"+v_cart.price)
								.replace(/##skuName##/g,v_cart.skuName)
								.replace(/##status##/g,v_status)
								.replace(/##count##/g,v_cart.count);
								/*.replace(/##skuId##/g,v_cart.id)
								.replace(/##subPrice##/g,v_cart.subPrice);*/
						$("#carList").html(v_res);

					}
					var v_cartHtml = $("#cartDiv").html().replace(/##totalCount##/g,v_data.totalCount)
							.replace(/##totalPrice##/g,'￥'+v_data.totalPrice);
					$("#goodsInfoDiv").html(v_cartHtml);

					$("#totalCountHtml").html('<div class="fc-price">应付金额:　<span class="price">¥'+v_data.totalPrice+'</span></div>')
					$("#lastGoogsInfoDiv").html(lastGoods);

				}else if(result.code==5100||result.code==5101||result.code==5102||result.code==5103){
					location.href="login.html";
				}
			}
		})
	}


	function findRecipientList(){
		$.ajax({
			type:"get",
			url:server_url+"/recipient/findRecipientList",
			beforeSend:function (xhr){
				let v_token=$.cookie("token");
				xhr.setRequestHeader("x-auth",v_token);
			},
			success:function (result){
				if(result.code==200){
					let  v_recipientList=result.data;
					var v_res="";
					for (let v_list of v_recipientList){
						var v_html = $("#recipientDiv").html();
						if(v_list.status==1){
							m_recipientId=v_list.id;
						}

						v_res += v_html.replace(/##name##/g,v_list.name)
								.replace(/##recipientAddress##/g,v_list.recipientAddress)
								.replace(/##phone##/g,v_list.phone)
								.replace(/##id##/g,v_list.id)
								.replace(/##selected##/g,v_list.status==1?"selected":"")
								.replace(/##span_addr##/g,v_list.status==1?"<span class=\"base\">默认地址</span>":"");

						if(v_list.status == 1){
							$("#defaultRecipientHtml").html('<div class="fc-receiverInfo">寄送至:'+v_list.name+' 收货人：'+v_list.recipientAddress+' '+v_list.phone+'</div>')
						}
					}
					$("#recipientList").html(v_res);
					initEvent();
				}
			}
		})
	}



	//弹框式增加
	function addAlert() {
		//备份
		var v_Html = $("#addAlert").html();

		//弹框
		var v_Dlg = bootbox.dialog({
			title: '增加地址',
			message: $("#addAlert form"),
			size: "large",
			buttons: {
				comfirm: {
					label: '<span class="glyphicon glyphicon-ok"></span>确认',
					className: 'btn-primary',
					callback: function () {
						//获取参数
						 var param = {}
						 param.name = $("#add_name", v_Dlg).val();
						 param.phone = $("#add_phone", v_Dlg).val();
						 param.recipientAddress = $("#add_recipientAddress", v_Dlg).val();

						//发送ajax请求，传递参数
						// console.log(param);
						$.ajax({
							url:server_url+"/recipient/addRecipient",
							beforeSend:function (xhr){
								let v_token=$.cookie("token");
								xhr.setRequestHeader("x-auth",v_token);
							},
							type: "post",
							data: param,
							success: function (result) {
								if (result.code == 200) {
									alert("新增成功！")
									findRecipientList()
								} else{
									alert(result.code)
									bootbox.alert({
										message: "<span class='glyphicon glyphicon-exclamation-sign'></span>新增失败",
										size: 'small',
										title: "提示信息"
									})
								}

							},error:function (res){
								alert("ajax发送异常")
							}
						});
					}

				},
				cancel: {
					label: '<span class="glyphicon glyphicon-remove"></span>取消',
					className: 'btn-danger',
				}
			}
		});
		//还原
		$("#addAlert").html(v_Html);
	}

	function delerecipient(id){
		var r=confirm("确认要删除吗")
		if(r==false){
			return
		}


		$.ajax({
			type:"delete",
			url:server_url+"/recipient/deleteRecipient",
			beforeSend:function (xhr){
				let v_token=$.cookie("token");
				xhr.setRequestHeader("x-auth",v_token);
			},
			data:{"id":id},
			success:function (result){
				console.log(result);
				if(result.code==200){
					findRecipientList();
				}else {
					alert(result.msg);
				}
			}
		})
	}


	//弹框式修改
	function modifyRecipient(id) {
		$.ajax({
			url:server_url+"/recipient/selectById",
			type: "get",
			beforeSend:function (xhr){
				let v_token=$.cookie("token");
				xhr.setRequestHeader("x-auth",v_token);
			},
			data: {"id": id},
			success: function (result) {
				if (result.code == 200) {
					var v_recipient = result.data;
					//回填数据
					$("#update_id").val(v_recipient.id);
					$("#update_name").val(v_recipient.name);
					$("#update_memberId").val(v_recipient.memberId);
					$("#update_name").val(v_recipient.name);
					$("#update_phone").val(v_recipient.phone);
					$("#update_recipientAddress").val(v_recipient.recipientAddress);

					//备份
					var vrecipientHtml = $("#updateAddresDiv").html();
					//弹框
					var v_updateAlert = bootbox.dialog({
						title: '修改',
						message: $("#updateAddresDiv form"),
						size: "large",
						buttons: {
							comfirm: {
								label: '<span class="glyphicon glyphicon-ok"></span>确认',
								className: 'btn-primary',
								callback: function () {
									//获取参数
									var v_id = $("#update_id", v_updateAlert).val();
									var v_memberId = $("#update_memberId", v_updateAlert).val();
									var v_name = $("#update_name", v_updateAlert).val();
									var v_phone = $("#update_phone", v_updateAlert).val();
									var v_recipientAddress = $("#update_recipientAddress", v_updateAlert).val();

									var param = {}
									param.id = v_id;
									param.memberId = v_memberId;
									param.name = v_name;
									param.phone = v_phone;
									param.recipientAddress = v_recipientAddress;

									$.ajax({
										url:server_url+"/recipient/updateRecipient",
										type: "post",
										data: param,
										beforeSend:function (xhr){
											let v_token=$.cookie("token");
											xhr.setRequestHeader("x-auth",v_token);
										},
										success: function (result) {
											if (result.code == 200) {
												findRecipientList();
											}
										}
									})
								}

							},
							cancel: {
								label: '<span class="glyphicon glyphicon-remove"></span>取消',
								className: 'btn-danger',
							}
						}
					});
					//还原
					$("#updateAddresDiv").html(vrecipientHtml);
				}
			}
		})

	}

</script>
</html>